<?php
class usersControl extends module {
	public $permissions = array('*'=>array(ROLE_ADMIN, ROLE_SUPERADMIN));
	public $title = "Users control";
	public $frame = "admin";
	
	public function run() {
		$query = "SELECT
			u.id,
			u.is_active,
			u.login AS '"._("Login")."',
			up.name AS '"._("User name")."',
			u.last_activity AS '"._("Last activity")."'
			FROM users AS u
			LEFT JOIN user_properties AS up ON up.user_id=u.id
			WHERE u.role!='".(($this->user->role == ROLE_SUPERADMIN)?"000":ROLE_SUPERADMIN)."'";
		new sqlTable($this, $query, $this->engine->getUrlParam(0));
		new pagination($this, $this->sql->totalRowsCount, $this->engine->getUrlParam(0));
	}

	private function createForm($title) {
		$form = new formConstructor($this, "usersControl", "", $title);
		$form->addField("login", _("Login (e-mail)"), new formString($GLOBALS['userPrimaryProperties']['login']));
		$form->addField("_password", _("Password"), new formString("/.{0,100}/"));
		$form->addField("is_active", _("Activated"), new formRadio(array('0'=>_("no"),'1'=>_("yes"))));
		$form->addField("_name", _("User name"), new formString("/^.{0,100}$/"));
		$form->addField("_company", _("Company"), new formString("/^.{0,100}$/"));
		$groups = $this->sql->getAssocFromDB("SELECT id, name FROM permissions");
		$form->addField("_group", _("Permissions group"), new formSelectOne($groups));
		$form->addField(null, _("Save"), new formSubmit());
		return $form;
	}

	private function save($form, $new=false) {
		if ($new && $form->getValue("_password") == "") {
			$form->addError("_password");
		}
		
		if (count($form->errors) == 0) {
			try {
				$postData1['login'] = $form->getValue("login");
				$postData1['is_active'] = $form->getValue("is_active");
				if ($form->getValue("_password") != "") {
					$postData1["password"] = md5($form->getValue("_password"));
				}

				$postData2['name'] = $form->getValue("_name");
				$postData2['company'] = $form->getValue("_company");
				$postData2['group'] = $form->getValue("_group");

				if ($new) {
					$this->sql->insertIntoTable("users", $postData1);
					$postData2["user_id"] = $this->sql->lastInsertId;
					$this->sql->insertIntoTable("user_properties", $postData2);
				} else {
					$this->sql->updateTable("users", $postData1, array('id' => intval($this->engine->getUrlParam(0))));
					$this->sql->updateTable("user_properties", $postData2, array('user_id' => intval($this->engine->getUrlParam(0))));
				}
				$this->engine->jump("/admin/usersControl/");
			} catch (Exception $e) {
				new pageAlert($this, $e->getMessage());
			}
		} else {
			new pageAlert($this, _("Wrong form filling"));
		}
	}

	public function create() {
		$form = $this->createForm(_("New user"));
		if ($form->submit()) {
			$this->save($form, true);
		} else {
			$form->setValue("is_active", "1");
		}
		$form->write();
	}

	public function edit() {
		$form = $this->createForm(_("Edit user data"));
		if ($form->submit()) {
			$this->save($form);
		} else {
			$form->setValues($this->sql->getRowFromDB("SELECT
				u.login AS login, u.is_active AS is_active, up.name AS _name, up.company AS _company, up.group AS _group
				FROM users AS u
				LEFT JOIN user_properties AS up ON up.user_id=u.id
				WHERE u.id=".intval($this->engine->getUrlParam(0))
			));
		}
		$form->write();
	}
	
	public function delete() {
		$this->sql->deleteFromTable("users", array("id" => intval($this->engine->getUrlParam(0))));
		$this->sql->deleteFromTable("user_properties", array("user_id" => intval($this->engine->getUrlParam(0))));
		$this->engine->jump("/admin/usersControl/");
	}
}
?>